/* Exercise 9.1 Morse States */ /* Created David Miles April 2006 */ /* Updated Ben Rowland Febuary 2014 */ #include // CONFIG1 #pragma config FOSC = HS // Oscillator Selection (HS Oscillator, High-speed crystal/resonator connected between OSC1 and OSC2 pins) #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) #pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled) // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config VCAPEN = OFF // Voltage Regulator Capacitor Enable (All VCAP pin functionality is disabled) #pragma config PLLEN = OFF // PLL Enable (4x PLL disabled) #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming) #define _XTAL_FREQ 19660800 // Defines the hardware crystal frequency allowing the delay function to work correctly #include "lcdlib.h" #include "utils.h" #define DOT_EVENT '.' // Values for the events #define DASH_EVENT '-' #define SPACE_EVENT 32 #define STUCK_EVENT '*' #define BUSY_STATE 'B' // Values for the states #define QUIET_STATE 'Q' #define DEBUG //Here is a test sequence for the morse code reader. I am sending three characters unsigned char test_sequence [] = { /* P */ DOT_EVENT, DASH_EVENT, DASH_EVENT, DOT_EVENT, SPACE_EVENT, /* I */ DOT_EVENT, DOT_EVENT, SPACE_EVENT, /* C */ DASH_EVENT, DOT_EVENT, DASH_EVENT, DOT_EVENT, SPACE_EVENT, 0x00 /* end marker */ } ; unsigned char morse_state = QUIET_STATE ; // State variable - set at QUIET void setup_hardware (void) { ANSELA = 0x00; //Set PortA to use digital inputs ANSELB = 0x00; TRISA = 0xFF; //PORTA set to inputs TRISB = 0x00; //PORTB set to all outputs } //We call the function to deal with an event. How it behaves depends on the current state void do_morse ( unsigned char event ) { #ifdef DEBUG lcd_print_ch ( event ) ; lcd_print_ch ( morse_state ) ; #endif switch ( morse_state ) //Use a switch to select the code for the current state { case BUSY_STATE : switch ( event ) //Now have a switch for each event which might happen in this busy state { case DOT_EVENT : //Store the dot here break ; case DASH_EVENT : //Store the dash here break ; case SPACE_EVENT : //Work out the code and move to QUIET state morse_state = QUIET_STATE ; break ; case STUCK_EVENT : //Go to quiet state morse_state = QUIET_STATE ; } case QUIET_STATE : //Now handle events in quiet state switch ( event ) { case DOT_EVENT : //Store the dot here move to BUSY state morse_state = BUSY_STATE ; break ; case DASH_EVENT : //Store the dash here move to BUSY state morse_state = BUSY_STATE ; break ; case SPACE_EVENT : //Do nothing break ; case STUCK_EVENT : //Do nothing break ; } } #ifdef DEBUG lcd_print_ch ( morse_state ) ; #endif } int main (void) { unsigned char i ; setup_hardware(); lcd_start(); for ( i = 0; test_sequence[i] != 0; i = i + 1 ) { do_morse(test_sequence[i]); } while (1) ; return 0; }